<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Controller {
    public $islogin = 0;
    public $view;
    public function  __construct() {
        if($this->islogin) $this->isLogin();
        $this->init();
    }
    public function init() {}
    public function isPost() {
        if(empty($_POST)) {
            return false;
        }
        return true;
    }
    public function render($template) {
        $smarty = new Tools_Dismarty($template);
        $ALL_TIME = LOAD_TIME ? all_time('end') : '';
        $this->view['ALL_TIME'] = $ALL_TIME;
        $smarty->view = $this->view;
        $smarty->displayShow();
    }
    public function getNum($name) {
        return intval($_GET[$name]);
    }
    public function getString($name) {
        return htmlspecialchars($_GET[$name],ENT_QUOTES);
    }
    public function postNum($name) {
        return intval($_POST[$name]);
    }
    public function postString($name) {
        return htmlspecialchars($_POST[$name],ENT_QUOTES);
    }
    public function isLogin() {
        $name = getCookies('__u');
        $pwd = getCookies('__o');
        $nowtime = getCookies('l_time');
        $key = getCookies('__k');
        $uid = getCookies('__d');
        if(!$name||!$pwd||!$nowtime||!$key) goUrl('/login');
        $md5_key = md5_file(SERVER_ROOT.'/config/core.dat');
        if(!$md5_key) alertMsg('MSG_001');
        $check_key = md5($uid.$name.$pwd.$md5_key.$nowtime);
        if($check_key==$key) {
            return true;
        }
        goUrl('/login');
    }
    public function setLogin($uid,$name,$pwd,$time='') {
        if(!$uid||!$name||!$pwd) return false;
        $time = !$time? time()+3600:$time;
        $md5_key = md5_file(SERVER_ROOT.'/config/core.dat');
        if(!$md5_key) alertMsg('MSG_001');
        $nowtime = time();
        $key = md5($uid.$name.$pwd.$md5_key.$nowtime);
        setCookies('__d',$uid,$time);
        setCookies('__u',$name,$time);
        setCookies('__o',$pwd,$time);
        setCookies('l_time',$nowtime,$time);
        setCookies('__k', $key,$time);
        return true;
    }
}
?>